﻿using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;

using FarseerPhysics.Dynamics.Contacts;

namespace Spacer {
	class Shield : Item {
		public Shield() {}

		public Shield( SolarSys world ) : base(world) {
			if( this.GetType() == typeof(Shield) ) Initialize();
		}

		public override void Initialize() {
			base.Initialize();

			Sprite = Main.Sprites["Shield"];
			DrawMode = DrawMode.Sprite;
			DrawLayer = -Layer.LAYER_1;

			Name = "Kinetic Shield";
			ShieldAmount = 1000f;
			ShieldMax = 1000f;
            ResourceWorth = 500;
		}

		// ----------

		public override bool CreateSaveState( EntitySaveState save ) {
			base.CreateSaveState( save );

			save["ShieldAmount"] = ShieldAmount;

			return true;
		}

		// ----------

		public override bool AllowPickup( Pawn ply ) {
			Shield shield = ply.FindInventoryItem<Shield>();
			
			if( shield != null ) {
				shield.ShieldAmount = ShieldMax;
				shield.DrawColor = new Color( 1f, 1f, 1f, 1f );
				return false;
			} else {
				return true;
			}
		}

		public override void OnPickup() {
			Parent = Owner;
			LocalPosition = Owner.LocalCenter;
			
			if( Owner is Fighter )
				DrawScale = new Vector2( 1.5f );
			else if( Owner is FighterMedium )
				DrawScale = new Vector2( 2f );
			else if( Owner is FighterLarge )
				DrawScale = new Vector2( 2.85f );

			DrawColor = new Color( 1f, 1f, 1f, ShieldAmount/ShieldMax );
		}

		public override float OnOwnerTakeDamage( float amount, Entity instigator, Entity inflictor, Vector2 pos, Vector2 dir ) {
			// Adjust shield and damage
			if( amount <= ShieldAmount ) {
				ShieldAmount -= amount;
				amount = 0f;
			} else {
				amount -= ShieldAmount;
				ShieldAmount = 0f;
			}

			// Update draw color
			DrawColor = new Color( 1f, 1f, 1f, ShieldAmount/ShieldMax );

			return amount;
		}

		public override void DrawItemCard( Vector2 pos, Vector2 size ) {
			Surface.DrawColor = DrawColor;

            Vector2 ItemDisplayPos = new Vector2( pos.X + size.X/2, pos.Y + Sprite.Size.Y );
            Vector2 ItemNamePos = new Vector2( pos.X + size.X/2, pos.Y + size.Y - 25 );

            Surface.DrawSprite( Sprite, ItemDisplayPos, new Vector2(58, 58), Vector2.Zero, 0f, 0 );
			Surface.DrawString( "DefaultSmall", ((int)(ShieldAmount/ShieldMax*100))+"%", ItemDisplayPos, new Vector2(0.5f,0.5f), 0f );
			Surface.DrawString( "DefaultSmall", Name, ItemNamePos, new Vector2(0.5f,0.5f), 0f );
		}

		public override void Draw() {
			if( Parent != null && Parent is Pawn )
				base.Draw();
		}

		// ----------
        
		public float ShieldAmount { get; set; }
		public float ShieldMax { get; set; }
	}

	class Shield_Pickup : Pickup {
		public Shield_Pickup( SolarSys world ) : base(world) {
			Sprite = Main.Sprites["Shield_Pickup"];
			DrawMode = DrawMode.Sprite;
			DrawColor = new Color( 1f, 1f, 1f, 1f );

			ItemType = typeof( Shield );
		}
	}

}
